package com.jcrj.service;

import com.jcrj.dao.FileDao;
import com.jcrj.entity.OFile;
import com.jcrj.entity.OUser;
import java.util.Date;
import java.util.List;

public class FileService {
	private FileDao fileDao;

	public FileDao getFileDao() {
		return this.fileDao;
	}

	public void setFileDao(FileDao fileDao) {
		this.fileDao = fileDao;
	}

	public void saveFile(OFile file) {
		this.fileDao.saveFile(file);
	}

	public OFile getFileById(int id) {
		return (OFile) this.fileDao.getObject(new OFile(), id);
	}

	public void updateFile(OFile file) {
		this.fileDao.updateFile(file);
	}

	public void deleteFile(OFile file) {
		this.fileDao.deleteFile(file);
	}

	public List<OFile> getFileList(String files) {
		if ((files != null) && (!"".equals(files))) {
			String[] fileArray = files.split("_");
			String file = "";
			for (int i = 0; i < fileArray.length; i++) {
				if ("".equals(file))
					file = file + fileArray[i];
				else {
					file = file + "," + fileArray[i];
				}
			}
			String hql = "from OFile o where o.id in(" + file + ")";
			return this.fileDao.findByHql(hql);
		}
		return null;
	}

	public void deleteFileById(String fileId) {
		if ((fileId != null) && (!"".equals(fileId))) {
			String hql = "delete from OFile o where o.id in (" + fileId + ")";
			this.fileDao.executeHql(hql);
		}
	}

	public List<OFile> findFileListById(int id) {
		String hql = "from OFile where foreign=" + id + " and name='"
				+ "onlineword" + "'";
		return this.fileDao.findByHql(hql);
	}

	public void saveOnlineWord(int id, OUser user, String path) {
		List list = findFileListById(id);
		OFile file = null;
		if ((list != null) && (!list.isEmpty()))
			file = (OFile) list.get(0);
		else {
			file = new OFile();
		}
		file.setName("onlineword");
		file.setCreateTime(new Date());
		file.setUrl(path);
		file.setForeign(Integer.valueOf(id));
		this.fileDao.saveOrUpdateFile(file);
	}
}